﻿'***********************************************************************
'Magic Chimney Imp (MCI)
'Copyright (C) 2011  David Frendin (david.frendin@gmail.com)
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>.
'***********************************************************************
Public Class frmCardPool

    Private _CardPoolSet As DataSet
    Private _LocalCardPool As CardPool

    Private Sub frmCardPool_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'AddToCardPool(Application.StartupPath & "\cardpool\master.xml")
        'Dim fi As New IO.DirectoryInfo(Application.StartupPath & "\cardpool\")
        'For Each xmlfile As IO.FileInfo In fi.GetFiles("*.xml")
        'AddToCardPool(xmlfile.FullName)
        'Next

        KryptonComboBoxFilterType.Items.Add(String.Empty)
        KryptonComboBoxFilterType.Items.Add("Artifact")
        KryptonComboBoxFilterType.Items.Add("Basic")
        KryptonComboBoxFilterType.Items.Add("Creature")
        KryptonComboBoxFilterType.Items.Add("Enchantment")
        KryptonComboBoxFilterType.Items.Add("Instant")
        KryptonComboBoxFilterType.Items.Add("Land")
        KryptonComboBoxFilterType.Items.Add("Legendary")
        KryptonComboBoxFilterType.Items.Add("Plane")
        KryptonComboBoxFilterType.Items.Add("Planeswalker")
        KryptonComboBoxFilterType.Items.Add("Snow")
        KryptonComboBoxFilterType.Items.Add("Sorcery")
        KryptonComboBoxFilterType.Items.Add("Tribal")
        KryptonComboBoxFilterType.Items.Add("World")

    End Sub

    Public Sub LoadCardPool(ByVal TheCardPool As CardPool)
        _CardPoolSet = TheCardPool.AsDataSet
        KryptonDataGridViewCardPool.AutoGenerateColumns = False
        KryptonDataGridViewCardPool.DataSource = _CardPoolSet.Tables(0)
    End Sub

    'Private Sub AddToCardPool(ByVal FilePath As String)
    '    Dim ds As New DataSet

    '    Try
    '        ds.ReadXml(FilePath)
    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '    End Try
    '    For Each row As DataRow In ds.Tables("card").Rows
    '        Dim card As String = row("name").ToString
    '        Dim cost As String = row("cost").ToString
    '        Dim cardtype As String = row("type").ToString
    '        Dim pt As String = row("pt").ToString
    '        Dim rules As String = row("rules").ToString
    '        Dim cardset As String = row("set").ToString
    '        Dim lvi As New ListViewItem
    '        lvi.Text = card
    '        lvi.SubItems.Add(cost)
    '        lvi.SubItems.Add(cardtype)
    '        lvi.SubItems.Add(pt)
    '        lvi.SubItems.Add(rules)
    '        lvi.SubItems.Add(cardset)
    '        ListViewCardPool.Items.Add(lvi)
    '    Next
    'End Sub

    Private Sub AddCardToDeckToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCardToDeckToolStripMenuItem.Click
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub

    Private Sub KryptonDataGridViewCardPool_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles KryptonDataGridViewCardPool.CellContentClick

    End Sub

    Private Sub KryptonDataGridViewCardPool_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles KryptonDataGridViewCardPool.CellDoubleClick
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub

    Private Sub TextBoxSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSearch.TextChanged
        TimerToSearch.Stop()
        TimerToSearch.Start()
    End Sub

    Private Sub TimerToSearch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerToSearch.Tick
        'Dim DataRows() As DataRow = _CardPoolSet.Tables(0).Select("name LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%' OR type LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%' OR rules LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%'")
        'If DataRows.Count > 0 Then
        '    KryptonDataGridViewCardPool.DataSource = DataRows.CopyToDataTable
        'Else
        '    KryptonDataGridViewCardPool.DataSource = Nothing
        'End If
        PerformSearch()
        TimerToSearch.Stop()
    End Sub

    Private Function MakeQuerySafe(ByVal Query As String) As String
        Query = Query.Replace("'", "''")
        Return Query
    End Function

    Private Sub KryptonDataGridViewCardPool_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonDataGridViewCardPool.SelectionChanged
        If KryptonDataGridViewCardPool.SelectedRows.Count > 0 Then
            Dim MultiverseId As String = KryptonDataGridViewCardPool.SelectedRows(0).Cells("MultiverseId").Value.ToString
            Dim MyCard As CardPool.Card = CardPool.GetCard(MultiverseId)

            CardPool.SetCardImage(PictureBox1, MyCard.MultiverseId)
        End If

    End Sub

    Private Sub KryptonCheckButtonFilterColorColorless_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorColorless.CheckedChanged
        If KryptonCheckButtonFilterColorColorless.Checked Then
            KryptonCheckButtonFilterColorBlack.Checked = False
            KryptonCheckButtonFilterColorBlue.Checked = False
            KryptonCheckButtonFilterColorRed.Checked = False
            KryptonCheckButtonFilterColorGreen.Checked = False
            KryptonCheckButtonFilterColorWhite.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub KryptonCheckButtonFilterColorBlack_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorBlack.CheckedChanged
        If KryptonCheckButtonFilterColorBlack.Checked Then
            KryptonCheckButtonFilterColorColorless.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub KryptonCheckButtonFilterColorBlue_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorBlue.CheckedChanged
        If KryptonCheckButtonFilterColorBlue.Checked Then
            KryptonCheckButtonFilterColorColorless.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub KryptonCheckButtonFilterColorGreen_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorGreen.CheckedChanged
        If KryptonCheckButtonFilterColorGreen.Checked Then
            KryptonCheckButtonFilterColorColorless.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub KryptonCheckButtonFilterColorWhite_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorWhite.CheckedChanged
        If KryptonCheckButtonFilterColorWhite.Checked Then
            KryptonCheckButtonFilterColorColorless.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub KryptonCheckButtonFilterColorRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonCheckButtonFilterColorRed.CheckedChanged
        If KryptonCheckButtonFilterColorRed.Checked Then
            KryptonCheckButtonFilterColorColorless.Checked = False
        End If
        FiltersChanged()
    End Sub

    Private Sub FiltersChanged()
        PerformSearch()
    End Sub

    Private Sub PerformSearch()
        Dim FilterString As String = String.Empty
        If KryptonCheckButtonFilterColorBlack.Checked Then
            FilterString = FilterString & " AND cost LIKE '%B%'"
        End If
        If KryptonCheckButtonFilterColorBlue.Checked Then
            FilterString = FilterString & " AND cost LIKE '%U%'"
        End If
        If KryptonCheckButtonFilterColorGreen.Checked Then
            FilterString = FilterString & " AND cost LIKE '%G%'"
        End If
        If KryptonCheckButtonFilterColorRed.Checked Then
            FilterString = FilterString & " AND cost LIKE '%R%'"
        End If
        If KryptonCheckButtonFilterColorWhite.Checked Then
            FilterString = FilterString & " AND cost LIKE '%W%'"
        End If
        If KryptonCheckButtonFilterColorColorless.Checked Then
            FilterString = " AND cost NOT LIKE '%B%' AND cost NOT LIKE '%U%' AND cost NOT LIKE '%G%' AND cost NOT LIKE '%R%' AND cost NOT LIKE '%W%'"
        End If

        If KryptonCheckBoxExcludeUnselected.Checked Then
            If Not KryptonCheckButtonFilterColorBlack.Checked Then
                FilterString = FilterString & " AND NOT cost LIKE '%B%'"
            End If
            If Not KryptonCheckButtonFilterColorWhite.Checked Then
                FilterString = FilterString & " AND NOT cost LIKE '%W%'"
            End If
            If Not KryptonCheckButtonFilterColorBlue.Checked Then
                FilterString = FilterString & " AND NOT cost LIKE '%U%'"
            End If
            If Not KryptonCheckButtonFilterColorRed.Checked Then
                FilterString = FilterString & " AND NOT cost LIKE '%R%'"
            End If
            If Not KryptonCheckButtonFilterColorGreen.Checked Then
                FilterString = FilterString & " AND NOT cost LIKE '%G%'"
            End If
        End If

        If Not KryptonComboBoxFilterType.Text = String.Empty Then
            FilterString = FilterString & " AND type LIKE '%" & KryptonComboBoxFilterType.Text & "%'"
        End If


        Dim DataRows() As DataRow = _CardPoolSet.Tables(0).Select("(name LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%' OR type LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%' OR rules LIKE '%" & MakeQuerySafe(TextBoxSearch.Text) & "%')" & FilterString)
        If DataRows.Count > 0 Then
            KryptonDataGridViewCardPool.DataSource = DataRows.CopyToDataTable
        Else
            KryptonDataGridViewCardPool.DataSource = Nothing
        End If
    End Sub

    Private Sub KryptonComboBoxFilterType_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles KryptonComboBoxFilterType.SelectedValueChanged
        PerformSearch()
    End Sub

    Private Sub KryptonCheckBoxExcludeUnselected_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonCheckBoxExcludeUnselected.CheckedChanged
        PerformSearch()
    End Sub
End Class